package edu.gdkm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gdkm.controller.request.BaseRequest;
import edu.gdkm.entity.Borrow;
import edu.gdkm.entity.Retur;
import edu.gdkm.mapper.BorrowMapper;
import edu.gdkm.service.IBorrowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class BorrowService implements IBorrowService {
    @Resource
    BorrowMapper borrowMapper;


    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Borrow> list() {
        return borrowMapper.list();
    }

    /**
     * 分页查询 模糊查询 PageHelper插件
     * @param
     * @return
     */
    @Override
    public PageInfo<Borrow> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        return new PageInfo<>(borrowMapper.listByCondition(baseRequest));
    }

    @Override
    public PageInfo<Retur> pageRetur(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        return new PageInfo<>(borrowMapper.listReturCondition(baseRequest));    }

    @Override
    public void save(Borrow obj) {
        borrowMapper.save(obj);
    }

    @Override
    public void saveRetur(Retur obj) {
        obj.setStatus("已归还");
        borrowMapper.updateStatus("已归还",obj.getId());
//        obj.setId(null);
        borrowMapper.saveRetur(obj);

    }

    @Override
    public Borrow getById(Integer id) {
        return borrowMapper.getById(id);
    }

    @Override
    public void update(Borrow obj) {
        borrowMapper.updateById(obj);
    }

    @Override
    public void deleteById(Integer id) {
        borrowMapper.deleteById(id);
    }

    @Override
    public void deleteReturById(Integer id) {
        borrowMapper.deleteReturById(id);

    }
}
